<template>
  <div id="coment">
    <h3>发表评论</h3>
    <hr>
    <textarea maxlength="200" placeholder="氛围和两位" v-model="getcoment"></textarea>
    <mt-button type="primary" size="large" @click="postcoment">发表评论</mt-button>
    <div class="cmt-list">
      <div class="cmt-item" v-for="(item,i) in arr" :key="item.add_time">
        <div
          class="cmt-title"
        >第{{i}}楼&nbsp;&nbsp;用户：{{item.user_name}}&nbsp;&nbsp;发表时间：{{item.add_time | myFilter}}</div>
        <div class="cmt-body">{{item.content===""?'此用户很懒':item.content}}</div>
      </div>
    </div>
    <mt-button type="primary" size="large" @click="getmore">加载更多</mt-button>
  </div>
</template>

<script>
import { Toast } from "mint-ui";
import qs from 'qs'
export default {
  data() {
    return {
      pageindex: 1,
      arr: [],
      getcoment: ""
    };
  },
  methods: {
    getcomment() {
      this.$axios
        .get("/api/getcomments/" + this.id + "?pageindex=" + this.pageindex)
        .then(res => {
          if (res.data.status == 0) {
            this.arr = this.arr.concat(res.data.message);
            console.log(this.id)
            console.log(this.arr)
          } else {
            Toast("数据获取失败");
          }
        });
    },
    getmore() {
      this.pageindex++;
      this.getcomment();
    },
    postcoment() {
      this.$axios
        .post("/api/postcomment/" + this.$route.params.id, {
          content: this.getcoment.trim()
        })
        .then(res => {
          if(this.getcoment.trim()==''){
            Toast("评论不能为空");
            return
          }
          if (res.data.status == 0) {
            let msg=qs.stringify({
              content:this.getcoment
            })

            //bug未解决
            let com={
              user_name:'匿名用户',
              add_time:Date.now(),
              content:msg
            }
            
            this.arr.unshift(com);
            this.getcoment=''
            // console.log(res);

          } else {
            Toast("评论失败");
          }
        });
    }
  },
  created() {
    this.getcomment();
  },
  components: {},
  props: ["id"]
};
</script>

<style scoped lang="scss">
#coment {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }
  .cmt-list {
    margin: 10px 0;
    .cmt-item {
      font-size: 13px;

      .cmt-title {
        line-height: 30px;
        background-color: #ccc;
      }
      .cmt-body {
        line-height: 350x;
        text-indent: 2em;
      }
    }
  }
}
</style>
